<?php

namespace Home\Controller;

use Think\Controller;

class OrderCourierController extends BaseController {

    /**
     * 快递员数据统计接口
     * @autho cheng
     */
    public function queryOrderCourier() {
        $param = I('param.');
        empty($param['courierId']) && $this->param_error(); //快递员ID必填

        $page = !empty($param['currentPage']) ? $param['currentPage'] : 1; //当前页
        $nums = !empty($param['pageSize']) ? $param['pageSize'] : 10; //页大小

        /***************************************************
         * preday为查看滞留件必传参数   date为选择的日期       *
         * 如果preday没有参数且date（日期）不为空，则以date为准 *
         * 如果preday和date同时为空，则默认查询两个月的包裹数据 *
         * preday根据传过来的参数进行转换                     *
         ***************************************************/

        $times = '';
        //根据preDay决定查询日期1.今日 2.昨日 3.3日以上
        if (empty($param['preDay']) && !empty($param['date'])) {
            $times = $param['date'];
        } elseif (!empty($param['preDay'])) {
            if ($param['preDay'] == 1){
                $dayTime = date("Y-m-d",strtotime('-'.$param['preDay']."day"));
                $endTime = $startTime = $dayTime;
            }elseif($param['preDay'] == 2){
                $dayTime = date("Y-m-d",strtotime('-'.$param['preDay']."day"));
                $endTime = $startTime = $dayTime;
            }else{
                $dayTime = date("Y-m-d",strtotime('-'.$param['preDay']."day"));
                $startTime =  date('Y-m', strtotime('-1 month', strtotime(date('Y-m-d', strtotime('-25 day', time()))))) . '-01';
                $endTime = $dayTime;
            }
        }
        elseif ( empty( $param['preDay'] ) && empty( $param['date'] ) )
        {
            $startTime =  date("Y-m-01",strtotime(" - 1 month"));
            $endTime = date( 'Y-m-d' );
        }

        $combinedNo = $param['combinedNo'];  //手机号/运单号

        $getMobile = $getmailNO = '';

        //正则验证匹配要查询的数据
        if( preg_match( '|^1\d{10}$|', $combinedNo ) ) {
            $getMobile = $combinedNo;
        }
        else {
            $getmailNO = $combinedNo;
        }

        //兼容老版本查询  0,2,5  状态包裹
        if ($param['state'] == '0,2' || $param['state'] == ''){
            $param['state'] = '-250';
        }

        //StationId存在则快递员id不生效  否者查询合作快递员下所有的包裹

        $parcelService = new \Home\Server\ParcelService();
        $count = $parcelService->countExpressStorage($param['courierId'], $getMobile,$times,$param['state'], $getmailNO, $startTime, $endTime,$param['stationId']);

        $list = $parcelService->getExpressStorage($param['courierId'], $getMobile,$times,$param['state'],$page,$nums, $getmailNO, $startTime, $endTime,$param['stationId']);

        //通知中通知失败数量显示
        $noticeIng = $parcelService->countExpressStorage( $param['courierId'] , $getMobile , $times , 2 , '' , $startTime , $endTime );
        $noticeFail = $parcelService->countExpressStorage( $param['courierId'] , $getMobile , $times , 5 , '' , $startTime , $endTime );

        $noticeIng == false ? $noticeIng = '0' : $noticeIng;
        $noticeFail == false ? $noticeFail = '0' : $noticeFail;
        $count == false ? $count = '0' : $count;
        $list == false ? $list = array() : $list;

        $data['noticeIng'] = $noticeIng;
        $data['noticeFail'] = $noticeFail;
        $data['totalPage'] = ceil($count/$nums);
        $data['totalCount'] = $count;
        $data['packetInfoList'] = $list;
        $this->output($status = 1, $data);

    }



}
